<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * 次卡项目管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择项目！','error');
	}
}
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_meye_timeserve',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$serve=$this->pdo_get('serve',array('id'=>$item['serve_id']),array('title','image'));
			$item['serveimage']=$serve['image'];
			$item['servetitle']=$serve['title'];
			$item['url']=custom_url('jy_meye/timeserver/sever_detail/id='.$item['id'],true);
		}
	}else{
		$list=array();
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'次卡列表'),
			array('op'=>'edit','title'=>'添加次卡'),
		),
		'list'=>array('data'=>'list','tip'=>'这里设置平台次卡，门店可以选择这里的次卡!','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'次卡名称','type'=>'string'),
			array('type'=>'img_string','title'=>'所属项目','img'=>'serveimage',
			'data'=>array(
					array('title'=>'名称：','name'=>'servetitle'),
				)
			),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'times','title'=>'次数','type'=>'string'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>'15','title'=>'项目名称','type'=>'string','width'=>'50'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_meye_timeserve',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['limittimes']=json_decode($item['limittimes'],true);
	}else{
		$item=array();
		$item['limittimes']=array();
	}
	$item['hdps']=$item['hdps']?json_decode($item['hdps'],true):array();
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回次卡列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本信息',
							'list'=>array(
								array('type'=>'string','title'=>'次卡名称','name'=>'title','help'=>''),
								array('type'=>'image','title'=>'次卡图片','name'=>'image','help'=>''),
								array('type'=>'choose','action'=>'jy_meye/serve/choose/','name'=>'serve_id','title'=>'选择服务项目','must'=>true,'read'=>$id?true:false),
								array('type'=>'string','title'=>'次卡价格','name'=>'price','help'=>''),
								array('type'=>'unitstring','unite'=>'次','title'=>'使用次数','name'=>'times','help'=>''),
								array('type'=>'radio','name'=>'timelimittype','title'=>'使用时间限制','options'=>'timelimittype'),
								array('type'=>'unitstring','unit'=>'天','name'=>'timedays','title'=>'购买后天数内有效','show'=>array('timelimittype'=>0)),
								array('type'=>'time_range','name'=>'limittimes','title'=>'在时间段内有效','show'=>array('timelimittype'=>1)),	
								array('type'=>'text','title'=>'项目说明','name'=>'remark','help'=>''),
								array('type'=>'switch','title'=>'开启','name'=>'status','help'=>''),
							)
						),
						array(
							'title'=>'幻灯片',
							'list'=>array(
								array(
									'type'=>'array',
									'name'=>'hdps',
									'add_tip'=>'添加一个幻灯片',
										'list'=>array(
											array('type'=>'image','name'=>'picurl','title'=>'幻灯片图片'),
											array('type'=>'string','name'=>'url','title'=>'幻灯片链接'),
											
										),
										'details'=>array(
											array('type'=>'image','name'=>'picurl','title'=>'幻灯片图片','help'=>''),
											array('type'=>'string','name'=>'url','title'=>'幻灯片链接'),
											
										)
							),
							)
						),
			)
	);
	$return['timelimittype']=$this->timelimittypes;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'price'=>trim($postdata['price']),
		'serve_id'=>intval($postdata['serve_id']),
		'times'=>intval($postdata['times']),
		'timelimittype'=>intval($postdata['timelimittype']),
		'timedays'=>intval($postdata['timedays']),
		'limittimes'=>ijson_encode($postdata['limittimes']),
		'remark'=>trim($postdata['remark']),
		'status'=>intval($postdata['status']),
		'paixu'=>intval($postdata['paixu']),
		'hdps'=>ijson_encode($postdata['hdps']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_meye_timeserve',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_meye_timeserve',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_meye_timeserve',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要设置的项目！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_meye_timeserve',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='choose'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array();
	$list=pdo_getslice('jy_meye_timeserve',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_meye/timeserve//id='.$item['id'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'次卡列表'),
		),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'title','title'=>'次卡名称','type'=>'string'),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
		),'action'=>array(
			array('type'=>'modal_result','name'=>'id','title'=>'选取'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string','width'=>'50'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}